# -*- coding: utf-8 -*-

import sqlite3
import sys

from model.toquin import Toquin
from model.toquinitem import ToquinItem

class ToquinSqlite3Reader:
    def read(self, db):
        toquinmodel = Toquin()
        connection = sqlite3.connect(db)
        connection.row_factory = sqlite3.Row
        cursor = connection.cursor()

        cursor.execute("SELECT ROWID AS ItemID, * FROM Instalables")

        for i in cursor:
            newitem = ToquinItem()
            
            newitem.id = i['ItemID']
            newitem.category = i['Categoria']
            newitem.name = i['Nombre']
            newitem.keywords = i['Palabra Clave']

            if i['Windows'] != None:
                newitem.version_windows = i['Windows']
                newitem.size_windows = i['Windows Tamaño']

            newitem.documentation = i['Manual']
            newitem.license = i['Licencia']
            newitem.description = i['Descripción']
            newitem.filename = i['Archivo']
            newitem.url = i['URL']

            toquinmodel.addItem(newitem)
            i = cursor.fetchone()

        return toquinmodel
